#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/wait.h>
#include <sys/types.h>

int main()
{   
    printf("process is running...\n");
    pid_t id=fork();
    assert(id!=-1);

    if(id==0)
    {
        //类比：命令行怎么写，这里就怎么写
        //sleep(1);
        //execl("/usr/bin/ls","ls","-a","-l","--color=auto",NULL);//execl第一个参数要带具体路径
        //execlp("ls","ls","-a","-l","--color=auto",NULL);//execlp第一个参数不用带具体路径
        /*char *const argv_[]
        {
            "ls","-a","-l","--color=auto",NULL
        };
        execv("/usr/bin/ls",argv_);//execv参数为指针数组*/

        //用myexec.c调用mybin.c程序
        execl("./mybin","mybin",NULL);
        exit(1);//must failed
    }

    int status=0;
    pid_t ret=waitpid(id,&status,0);
    if(ret>0)
    {
        printf("wait success:exit code:%d,sig:%d\n",(status>>8)&0xFF,status & 0x7F);
    }




    //printf("process is running...\n");

    //excel只有在出错时才有返回值
    //execl("/usr/bin/ls"/*要执行哪一个程序*/,"ls","--color=auto","-a","-l",NULL/*你想怎么执行*/);
    //execl("/usr/bin/top"/*要执行哪一个程序*/,"top",NULL/*你想怎么执行*/);

    //perror("execl");
    //为什么这里的printf没有执行？
    //printf也是代码，实在execl之后的，execl执行完毕后，代码已经全部被覆盖，开始执行新的代码了，所以printf就无法执行了!
    //printf("process running done..\n");
    //exit(1);
}
